<template>
  <div class="user-list">
    <div style="height: 60px">
      <el-button type="primary" @click="addUserData" style="margin: 5px;float: right">新增</el-button>
    </div>
    <el-table
      stripe
      height="800"
      :data="userDataList"
      style="width: 100%"
    >
      <el-table-column
        align="center"
        label="用户名称"
        width="350"
        prop="user_name"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="登录名称"
        width="350"
        prop="login_name"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="用户类型"
        prop="group_name"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="最后登录时间"
        prop="last_sign_date"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="最后登录ip"
        prop="last_login_ip"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="所属企业"
        prop="enterprise_name"
      >
        <template slot-scope="{row}">
          <span @click="goDeviceList(row)"><a style="color: blue">{{row.enterprise_name}}</a></span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        width="200"
        label="操作"
        prop="handler"
      >
        <template slot-scope="{row}">
          <span @click="editUserClick(row)"><svg-icon class="handler-icon" icon-class="edit"/></span>
          <span @click="delUserClick(row.device_id)"><svg-icon class="handler-icon" icon-class="right"/></span>
        </template>
      </el-table-column>
    </el-table>
    <user-form ref='user_from_dialog'/>
  </div>
</template>

<script>
  import userForm from './UserForm'

  export default {
    name: "UserList",
    components: {userForm},
    methods: {
      queryData(){
        let me = this;
        this.$axios.post('userConfigRoutes', {
          action_type: 'userQuery',
        }).then(function (response) {
          me.userDataList = response.data.data;
          return {success : true}
        });
      },
      editUserClick(record) {
        let formDate = JSON.parse(JSON.stringify(record));//由于vue双向绑定的缘故，form表单中数据变换会营销到表格中的数据同时变换，因此这里需要进行一次转换操作避免绑定情况出现
        this.$refs.user_from_dialog.setForm(formDate);
      },

      delUserClick(device_id) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let me = this;
          this.$axios.post('userConfigRoutes',{
            action_type: 'userDel',
            device_id:device_id,
          }).then(function (response) {
            if(response.data.success) me.$message({type: 'success',message: '删除成功!'});
            else me.$message({type: 'error',message: '删除失败!'});
            me.queryData();
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      addUserData() {
        this.$refs.user_from_dialog.setForm();
      },

      goDeviceList(row){
        this.$router.push({name: 'deviceList', params: {user_id: row.user_id,enterprise_id:row.enterprise_id}})
      }
    },
    data() {
      return {
        userDataList : [],
      }
    },
    created() {
      this.queryData();
    },


  }
</script>

<style lang="scss" scoped>
  .plcDevice-list {
    background-color: #fff;
  }
  a:hover{cursor:pointer}
</style>
